package com.whz.http.client;

import com.whz.generic.spring.SpringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.cloud.openfeign.clientconfig.HttpClientFeignConfiguration;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 自定义httpclient
 *
 * @author whz
 */
@Configuration
@AutoConfigureBefore(HttpClientFeignConfiguration.class)
public class HttpClientPoolConfiguration {

    private int socketTimeout = 5000;
    @Resource
    FeignHttpClientProperties feignHttpClientProperties;

    @Bean(destroyMethod = "close")
    public CloseableHttpClient createTongyuCloseableHttpClient() {
        Optional<Environment> env = SpringUtils.currentEnvironment();
        if (env.isPresent()) {
            String val = env.get().getProperty("feign.httpclient.socketTimeout", "5000");
            socketTimeout = Integer.parseInt(val);
        }
        return new DefaultHttpClient(socketTimeout, feignHttpClientProperties).client();
    }
}